<?php
	$qrCheck = $GLOBALS['MYT']->db_array("SELECT iveID FROM systemoffice.inventory_entries WHERE iveInventory = 1 AND iveDepartment = {$_POST['iveDepartment']}");
	if (!isset($qrCheck[1])) {
	$qr = 'INSERT INTO systemoffice.inventory_entries (iveCat, iveDepartment, iveDeptReference, iveInventory, iveEntryDate, iveEntryTime, iveEntryUser, iveQuant, ivePrice, iveItem, iveDescription) VALUES ';
	for ($i=1;$i<=$_POST['iveTotal'];$i++){
		if($_POST['iveQuant'.$i] == '') {$_POST['iveQuant'.$i] = 0; }
			$qr .= "({$_POST['iveCat'.$i]}, {$_POST['iveDepartment']},{$_POST['iveItem'.$i]}, 1, DATE('{$sysTime}'),TIME('{$sysTime}'),{$_SESSION[$SESSID]}, {$_POST['iveQuant'.$i]}, 0, {$_POST['iveItem'.$i]}, '{$_POST['iveDescription'.$i]}'),";
	}
	if ($_POST['iveTotal'] > 0) {
	$qr = substr($qr,0,strlen($qr)-1);
	$GLOBALS['MYT']->db_query($qr);
	}
		echo 'Guardado';
	} else {
		$qr = "UPDATE systemoffice.inventory_entries SET ";
		for ($i=1;$i<=$_POST['iveTotal'];$i++){
			if(!isset($qrCheck[$i])) {
			$GLOBALS['MYT']->db_query("INSERT INTO systemoffice.inventory_entries (iveCat, iveDepartment, iveDeptReference, iveInventory, iveEntryDate, iveEntryTime, iveEntryUser, iveQuant, ivePrice, iveItem, iveDescription) VALUES ({$_POST['iveCat'.$i]}, {$_POST['iveDepartment']},{$_POST['iveItem'.$i]}, 1, DATE('{$sysTime}'),TIME('{$sysTime}'),{$_SESSION[$SESSID]}, {$_POST['iveQuant'.$i]}, 0, {$_POST['iveItem'.$i]}, '{$_POST['iveDescription'.$i]}')");
			} else {
			$qr .= "iveQuant = IF(iveItem={$_POST['iveItem'.$i]},{$_POST['iveQuant'.$i]},iveQuant),iveDescription = IF(iveItem={$_POST['iveItem'.$i]},'{$_POST['iveDescription'.$i]}',iveDescription),";
			}
		}
		$qr = substr($qr,0,strlen($qr)-1);
		$qr .= " WHERE iveDepartment = {$_POST['iveDepartment']} AND iveInventory = 1";
		$GLOBALS['MYT']->db_query($qr);
		echo '<script charset="utf-8" type="text/javascript">alert(\'Actualizado Inventario ja existente.\');</script>';
		
	}